//
//  ModifyAccoutSafetyVC.m
//  QuHUa
//
//  Created by 顾梦 on 2017/12/7.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "ModifyAccoutSafetyVC.h"
#import "ModifyAccountSafetyTableViewCell.h"
#import "BorrowDetailThirdTableViewCell.h"
#import "ForgetPWDTableViewCell.h"
#import "ZZLCRegisterVC.h"//其实目的是为了获取验证码复用这个界面

@interface ModifyAccoutSafetyVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic)UITableView *tableView;

@end

@implementation ModifyAccoutSafetyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.titleStr;
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view from its nib.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell isKindOfClass:NSClassFromString(@"ModifyAccountSafetyTableViewCell")]) {
        ModifyAccountSafetyTableViewCell *mCell = (id)cell;
        [mCell.inputTF becomeFirstResponder];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {

        case 3:
        {
            return 105;
        }
            break;
            
        default:
            return 55;
            break;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        K_WS(weakSelf);
    switch (indexPath.row) {
        case 4:
        {
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ForgetPWDTableViewCell" owner:self options:nil];
            ForgetPWDTableViewCell *cell = [nibs firstObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = COLORE_GLOBLE_BACKGOUNDCOLOR;
            [cell.forgetPwdLabel bk_whenTapped:^{
                ZZLCRegisterVC *getCodeVC = [[ZZLCRegisterVC alloc]initWithNibName:@"ZZLCRegisterVC" bundle:nil];
                if ([self.title isEqualToString:@"修改登录密码"]) {
                    getCodeVC.getValidationCode = GetValidationCodeFormLoginPWD;
                }
                if ([self.title isEqualToString:@"修改交易密码"]) {
                    getCodeVC.getValidationCode = GetValidationCodeFormTradePWD;
                }
                [weakSelf.navigationController pushViewController:getCodeVC animated:YES];
            }];
            return cell;
        }
        case 3:
        {
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"BorrowDetailThirdTableViewCell" owner:self options:nil];
            BorrowDetailThirdTableViewCell *cell = [nibs firstObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.stateBTN setTitle:@"确认" forState:UIControlStateNormal];
            [cell.stateBTN bk_whenTapped:^{
                [self updatePWD];
            }];
            return cell;
        }
            break;
            
        default:
        {
            NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ModifyAccountSafetyTableViewCell" owner:self options:nil];
            ModifyAccountSafetyTableViewCell *cell = [nibs firstObject];
            switch (indexPath.row) {
                case 0:
                {
                    cell.indicateCategoryLabel.text = @"请输入密码";
                    if ([self.titleStr isEqualToString:@"修改交易密码"]) {
                        cell.inputTF.placeholder =@"请输入密码";
                    }
                }
                    break;
                case 1:
                {
                    cell.indicateCategoryLabel.text = @"请输入新密码";
                    cell.inputTF.placeholder = @"请输入新密码";
                }
                    break;
                default:
                {
                    cell.indicateCategoryLabel.text = @"请确认新密码";
                    cell.inputTF.placeholder = @"请保持两次输入一致";
//                    cell.getCodeBtn.hidden = NO;
                }
                    break;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
             cell.inputTF.secureTextEntry = YES;
            cell.inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
            if ([self.title isEqualToString:@"修改交易密码"]) {
                cell.inputTF.delegate = self;
                cell.inputTF.keyboardType = UIKeyboardTypeNumberPad;
            }
            return cell;
        }
            break;
    }
}
- (void)updatePWD
{

    ModifyAccountSafetyTableViewCell *oldPWDcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSString *oldPwd = oldPWDcell.inputTF.text;
    ModifyAccountSafetyTableViewCell *pwdcell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        NSString *pwd = pwdcell.inputTF.text;
    ModifyAccountSafetyTableViewCell *pwdVadidatecell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    NSString *pwdVadidate = pwdVadidatecell.inputTF.text;
    
    //    判断
    if (pwd.length<6&&pwd.length>16) {
        [QMUITips showError:@"请输入6-16位数字/字母/符号密码" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![pwd isEqualToString:pwdVadidate]) {
        [QMUITips showError:@"两次密码不一致" inView:self.view hideAfterDelay:1.2];
        return;
    }
    if (![pwd JudgeTheillegalCharacter]) {
        [QMUITips showError:@"密码格式错误" detailText:@"请输入6-16位数字/字母/符号密码"  inView:self.view hideAfterDelay:1.2];
        return;
    }
//
    NSLog(@"+++%@",self.typeStr);
    
    [NetWorkingManager postWithRequestUrl:@"User/updatePwd" AndParameters:@{@"parameters":[@{@"username":[UserModel userName],@"oldpwd":oldPwd,@"password":pwd,@"pwd":pwdVadidate,@"type":self.typeStr,@"authorization":[UserModel authorization],@"phoneId":[ZZHelper deviceIPAdress]} DataTOjsonStringAndAddVersion]} AndRequestBlock:^(NSDictionary *responseObject) {
      [QMUITips showSucceed:responseObject[@"message"] inView:[UIApplication sharedApplication].keyWindow hideAfterDelay:1.8];
   
        if ([self.title isEqualToString:@"修改登录密码"]) {
            [UserModel clearData];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        if ([self.title isEqualToString:@"修改交易密码"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
//
    } FailBlock:^(NSDictionary *errDic) {
        
    }];
}
#pragma mark lazy
- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]init];
        self.tableView.backgroundColor = COLORE_GLOBLE_BACKGOUNDCOLOR;
        _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *lastStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //    textField.text = lastStr;
    if (lastStr.length>6) {
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
